package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.SysUserDeptDto;
import com.ysd.lis.entity.SysOrgDepartment;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.SysUserDepartment;
import com.ysd.lis.mapper.sys.SysUserDepartmentMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysUserDepartmentService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户报告单元关系表 服务实现类
 * </p>
 *
 * @author HuiXing
 * @since 2024-01-02
 */
@Service
public class SysUserDepartmentServiceImpl extends ServiceImpl<SysUserDepartmentMapper, SysUserDepartment> implements SysUserDepartmentService {

    @Autowired
    private SysUserDepartmentMapper sysUserDepartmentMapper;

    public Result findSysUserDepartmentList(String userId) {
        LambdaQueryWrapper<SysUserDepartment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserDepartment::getUserId, userId);
        lambdaQueryWrapper.eq(SysUserDepartment::getDelFlag, 0);
        List<SysUserDepartment> sysUserDepartments = sysUserDepartmentMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", sysUserDepartments);
    }

    @Override
    public Result addSysUserDepartment(SysUserDepartment sysUserDepartment) {
        boolean save = this.save(sysUserDepartment);
        if (save) {
            return Result.succ(1, "添加成功", save);
        } else {
            return Result.fail("添加成功");
        }
    }

    @Override
    public Result addBatchSysUserDepartment(List<SysUserDepartment> sysUserDepartments) {
        boolean save = this.saveBatch(sysUserDepartments);
        if (save) {
            return Result.succ(1, "添加成功", save);
        } else {
            return Result.fail("添加成功");
        }
    }

    @Override
    public Result deleteSysUserDepartment(SysUserDepartment sysUserDepartment) {
        LambdaQueryWrapper<SysUserDepartment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(sysUserDepartment.getDeptId()),SysUserDepartment::getDeptId, sysUserDepartment.getDeptId());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(sysUserDepartment.getUserId()),SysUserDepartment::getUserId, sysUserDepartment.getUserId());
        int delete = sysUserDepartmentMapper.delete(lambdaQueryWrapper);
        if (delete >= 0) {
            return Result.succ(1, "添加成功", delete);
        } else {
            return Result.fail("添加成功");
        }
    }

    @Override
    public Result findUserDepartmentList(CommonPageDto pageParam) {
        MPJLambdaWrapper<SysUserDepartment> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(SysUserDepartment.class)
                .selectAs("ld", SysUser::getUsername,SysUserDepartment::getUsername)
                .selectAs("ld", SysUser::getRealname,SysUserDepartment::getRealname)
                .selectAs("ld", SysUser::getTel,SysUserDepartment::getTel)
                .selectAs("ld", SysUser::getEmail,SysUserDepartment::getEmail)
                .selectAs("ls", SysOrgDepartment::getName,SysUserDepartment::getDeptName)
                .leftJoin(SysUser.class,"ld",SysUser::getId,SysUserDepartment::getUserId)
                .leftJoin(SysOrgDepartment.class,"ls",SysOrgDepartment::getId,SysUserDepartment::getDeptId);

        if(ToolsUtils.isNotEmpty(pageParam.getPid())){
            queryWrapper.eq(SysUserDepartment::getDeptId,pageParam.getPid());
        }
        if(ToolsUtils.isNotEmpty(pageParam.getUserId())){
            queryWrapper.eq(SysUserDepartment::getUserId,pageParam.getUserId());
        }
        queryWrapper.eq(SysUserDepartment::getDelFlag,0)
                .orderByAsc(SysUserDepartment::getCreateTime);
        List<SysUserDepartment> itemList = sysUserDepartmentMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", itemList);
    }

    @Override
    public Result batchDeleteUserDepartmentList(SysUserDeptDto dto) {
        List<SysUserDepartment> sysUserDepartmentList = dto.getSysUserDepartmentList();
        List<String> ids = sysUserDepartmentList.stream().map(SysUserDepartment::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result batchAddSysUserDepartList(SysUserDeptDto dto) {
        List<SysUserDepartment> sysUserDepartmentList = dto.getSysUserDepartmentList();
        boolean b = this.saveBatch(sysUserDepartmentList);
        if(b){
            return Result.succ(1, "添加成功", null);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }
}
